package com.scuec.groupfive.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scuec.groupfive.entity.UserFeedback;
import com.scuec.groupfive.mapper.FeedbackMapper;
import com.scuec.groupfive.service.FeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class FeedbackServiceImpl implements FeedbackService {

    private final FeedbackMapper feedbackMapper;

    // 使用构造器注入替代字段注入，避免NullPointerException
    @Autowired
    public FeedbackServiceImpl(FeedbackMapper feedbackMapper) {
        this.feedbackMapper = feedbackMapper;
    }

    @Override
    public List<UserFeedback> getFeedbackList() {
        return feedbackMapper.getFeedbackList();
    }

    @Override
    public PageInfo<UserFeedback> getFeedbackPages(Integer pageNum, Integer pageSize) {
        // 设置默认值，避免空指针
        int pn = (pageNum == null || pageNum < 1) ? 1 : pageNum;
        int ps = (pageSize == null || pageSize < 1) ? 10 : pageSize;

        PageHelper.startPage(pn, ps);
        List<UserFeedback> feedbackList = feedbackMapper.getFeedbackList();
        return new PageInfo<>(feedbackList);
    }

    @Override
    public int insertFeedback(UserFeedback feedback) {
        // 确保设置username（这里需要实际业务逻辑，示例用固定值）
        if(feedback.getUsername() == null) {
            // 实际项目应从登录上下文/用户服务获取真实用户名
            feedback.setUsername("default_username");
        }

        feedback.setStatus("pending");
        return feedbackMapper.insertFeedback(feedback);
    }
    @Override
    public List<UserFeedback> getFeedbackByUserId(String userId) {
        return feedbackMapper.getFeedbackByUserId(userId);
    }
}